package Action.Manager;

import Action.Sys.Setting;
import Bean.Log.HistoricalData;
import Bean.Log.SystemLogEntity;
import Bean.Log.UserLogEntity;
import Bean.UserinfoEntity;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import tools.HibernateTools;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class AddUser extends ActionSupport {
    HttpServletRequest request;
    String err="";
    private String username;
    private String check_password;
    private String password;
    private String name;
    private String phone;
    private Integer age;
    @Override
    public String execute() throws Exception {
        clearErrorsAndMessages();

        request = ServletActionContext.getRequest();
        //检查管理员权限--委托给拦截器
//        if (!checkManage()) {
////            addActionError("权限不足,请您先登录");
//            request.getSession().setAttribute("err", "权限不足,请您先登录");
//            request.getSession().setAttribute("message", "");
////            SendUserData();
//            return LOGIN;
//        }
        //数据获取--已经改为通过struts自动获取了
//       getRequestData();

        if (username==null){
            //首次打开
            return INPUT;
        }
        //数据合法性检查
        if (!UserDataCheck()){
//            addActionError(err);
            request.getSession().setAttribute("err", err);
            SendUserData();
            request.getSession().setAttribute("message", "");
            return INPUT;
        }
        //数据封装
        UserinfoEntity userinfoEntity=getUserinfoEntity();
       //修改数据库
        Session session = HibernateTools.getSession();
        session.save(userinfoEntity);


        request.getSession().setAttribute("message", "用户添加成功");
        request.getSession().setAttribute("err", err);
        request.getSession().setAttribute("code", true);

        HistoricalData historicalData=session.get(HistoricalData.class, tools.TimeTools.formatTime(0));
        if (historicalData==null){
            historicalData=new HistoricalData();
            historicalData.setTimestamp(tools.TimeTools.formatTime(0));
        }
        historicalData.setNewUserNum(historicalData.getNewUserNum()+1);
        session.saveOrUpdate(historicalData);


        UserLogEntity.addLog(userinfoEntity,this);//用户自主注册
        SystemLogEntity.addLog((Integer) ServletActionContext.getRequest().getSession().getValue("adminid"), this, Setting.SystemLog_flag_AddUser);
        HibernateTools.commit(session);
        return SUCCESS;
    }
    //将用户填写的数据回写
    private void SendUserData() {
        HttpSession httpSession = request.getSession();
        httpSession.setAttribute("username",username);
        httpSession.setAttribute("check_password",check_password);
        httpSession.setAttribute("name",name);
        httpSession.setAttribute("age",age);
        httpSession.setAttribute("password",password);
        httpSession.setAttribute("phone",phone);

    }

    private boolean UserDataCheck() {
        //数据完整度检测
        if (username==null||username.equals(""))
            err+="请输入用户名\n";
        if (password==null||password.equals(""))
            err+="请输入密码\n";
        if (check_password==null||!check_password.equals(password))
            err+="两次密码不一致\n";
        if (password==null||password.length()<6)
            err+="密码位数需要大于等于6\n";
        if (phone==null||!tools.TelCheckTools.isValidPhoneNumber(phone))
            err+="请输入正确的电话号码";
        if (err.length()>0)
            return false;
        //用户合法性检测
        Session session=HibernateTools.getSession();
        if (session.createQuery("from UserinfoEntity user where username= ?").setParameter(0, username).list().size()!=0) {
            err+="用户名重复,请更换";
            return false;
        }
        return true;
    }

    private boolean checkManage() {

        return true;
    }

    public void getRequestData() {
        username = request.getParameter("username");
        check_password = request.getParameter("check_password");
        name = request.getParameter("name");
        try {
            age = Integer.valueOf(request.getParameter("age"));
        }
        catch (Exception e){
            System.err.println(age+"不是数字");
            age=0;
        }
        password = request.getParameter("password");
        phone = request.getParameter("phone");
    }

    public UserinfoEntity getUserinfoEntity() {
        UserinfoEntity userinfoEntity=new UserinfoEntity();
        userinfoEntity.setName(name);
        userinfoEntity.setUsername(username);
        userinfoEntity.setAge(Integer.valueOf(age));
        userinfoEntity.setPassword(password);
        userinfoEntity.setPhone(phone);
        return userinfoEntity;
    }


    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getCheck_password() {
        return check_password;
    }

    public void setCheck_password(String check_password) {
        this.check_password = check_password;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
